<?php
//焦点图
//@author yuanjiang 932625974#qq.com
//@date 10.9.2016
if(!defined('IN_T')){
   die('hacking attempt');
}

$act = Common::sfilter($_REQUEST['act']);

//详情: 查看或编辑或增加
if($act=='detail'){
   //id
   $sid = intval($_REQUEST['sid']);
   //传递了sid，验证sid是否存在，不存在强制赋值为0
   if($sid>0){
      $slide = $Db->query("select * from ".$Base->table('partner')." where id=$sid","Row");
      if(!$slide){
         $sid = 0;
      }
   }
   //前台查看
   if(empty($_POST)){
      $tp->assign('slide',$slide);
   }
   //后台处理
   else{
      $data = array(
         'partner_name' => Common::sfilter($_POST['partner_name']),
         'partner_path' => Common::sfilter($_POST['partner_path']),
         'partner_link' => trim($_POST['link']),
         'partner_sort' => intval($_POST['partner_sort']),
      );
      $res['status'] = 0;
      if(empty($data['partner_name'])){
         $res['msg'] = '请填写图片名称';
      }
      else if(empty($data['partner_path'])){
         $res['msg'] = '图片地址不正确';
      }
      else if(!empty($data['partner_link']) && !Common::is_url($data['partner_link'])){
         $res['msg'] = '链接地址不正确';
      }
      else{
         if($sid>0){
            $data['id'] = $sid;
            //替换，删除原图
            if($data['partner_path']!=$slide['partner_path']){
               chdir(ROOT_PATH);
               @unlink(substr($slide['partner_path'],1));
            }
         }
         $Db->replace($Base->table('partner'),$data);
         $res = array('status'=>1,'msg'=>'提交成功','href'=>'/'.ADMIN_PATH.'/?m=partner');
      }
      echo $Json->encode($res);
      exit;
   }
   $tp->assign('act','detail');
}
else if($act=='delete'){
   //id
   $sid = intval($_REQUEST['sid']);
   $res['status'] = 0;
   $img_path = $Db->query("select partner_path from ".$Base->table('partner')." where id=$sid","One");
   chdir(ROOT_PATH);
   @unlink(substr($img_path,1)); 
   $Db->delete($Base->table('partner'),array('id'=>$sid));
   $res = array('status'=>1,'msg'=>'删除成功！');
   echo $Json->encode($res);
   exit;
}
//列表 
else{
   $list = $Db->query("select * from ".$Base->table('partner')." order by partner_sort asc, id desc");
   $tp->assign('list',$list);
   $tp->assign('act','list');
}
$tp->assign('nav','合作伙伴');
?>